

package com.hazelcast.map.impl.operation;


import com.hazelcast.logging.ILogger;
import com.hazelcast.map.impl.MapContainer;
import com.hazelcast.map.impl.PartitionContainer;
import com.hazelcast.spi.exception.DistributedObjectDestroyedException;
import com.hazelcast.spi.impl.AllowedDuringPassiveState;
import com.hazelcast.spi.impl.operationservice.PartitionAwareOperation;
import com.hazelcast.spi.tenantcontrol.TenantControl;

import java.util.logging.Level;

/**
 * Operation to destroy the map data on the partition thread
 */
public class MapPartitionDestroyOperation extends AbstractMapLocalOperation implements PartitionAwareOperation, AllowedDuringPassiveState {

    private final PartitionContainer partitionContainer;

    public MapPartitionDestroyOperation(PartitionContainer partitionContainer, MapContainer mapContainer) {
        super(mapContainer);
        this.createRecordStoreOnDemand = false;
        this.partitionContainer = partitionContainer;
        this.mapContainer = mapContainer;
    }

    @Override
    protected void runInternal() {
        if (mapContainer == null) {
            // no such map exists
            return;
        }
        partitionContainer.destroyMap(mapContainer);
    }

    @Override
    public void logError(Throwable e) {
        if (e instanceof DistributedObjectDestroyedException) {
            ILogger logger = getLogger();
            logger.log(Level.FINEST, e.getMessage());
        } else {
            super.logError(e);
        }
    }

    @Override
    public boolean validatesTarget() {
        return false;
    }

    @Override
    public TenantControl getTenantControl() {
        return TenantControl.NOOP_TENANT_CONTROL;
    }
}
